package com.kede.system.rest;

import com.kede.annotation.Log;
import com.kede.system.domain.SysVirtual;
import com.kede.system.service.SysVirtualService;
import com.kede.system.service.dto.SysVirtualQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-04-18
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/sysVirtual")
public class SysVirtualController {

    private final SysVirtualService sysVirtualService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysVirtual:list')")
    public void exportSysVirtual(HttpServletResponse response, SysVirtualQueryCriteria criteria) throws IOException {
        sysVirtualService.download(sysVirtualService.queryAll(criteria), response);
    }

    /**
    * 查询virtual
    **/
    @GetMapping
    @Log("查询virtual")
    @PreAuthorize("@el.check('sysVirtual:list')")
    public ResponseEntity<Object> querySysVirtual(SysVirtualQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sysVirtualService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增virtual
    **/
    @PostMapping
    @Log("新增virtual")
    @PreAuthorize("@el.check('sysVirtual:add')")
    public ResponseEntity<Object> createSysVirtual(@Validated @RequestBody SysVirtual resources){
        return new ResponseEntity<>(sysVirtualService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改virtual
    **/
    @PutMapping
    @Log("修改virtual")
    @PreAuthorize("@el.check('sysVirtual:edit')")
    public ResponseEntity<Object> updateSysVirtual(@Validated @RequestBody SysVirtual resources){
        sysVirtualService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除virtual
    **/
    @DeleteMapping
    @Log("删除virtual")
    @PreAuthorize("@el.check('sysVirtual:del')")
    public ResponseEntity<Object> deleteSysVirtual(@RequestBody String[] ids) {
        sysVirtualService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}